package restful.servlet;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.ws.rs.*;

public class ResolverServlet extends HttpServlet {
	
	private ResponderFactory responderFactory;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse toResponse) throws ServletException, IOException {
		final String contextPath = request.getContextPath();
		responderFactory = new ResponderFactory(contextPath);
		final Responder responder = responderFactory.makeResponder(verb(request), request);
		responder.dispatch(request, toResponse);
	}
	
	protected Class verb(HttpServletRequest request) {
		return verb(request.getMethod());
	}

	public Class verb(String method) {
		if(method.equalsIgnoreCase("POST")) return POST.class;
		if(method.equalsIgnoreCase("GET"))return GET.class;
		if(method.equalsIgnoreCase("PUT"))return PUT.class;
		if(method.equalsIgnoreCase("DELETE"))return DELETE.class;
		throw new RuntimeException("unknown verb " + method);
	}
	
}
